package com.dmap.analysispoint.controller.api;

import com.dmap.analysispoint.bo.BurialPointBo;
import com.dmap.analysispoint.constants.BurialPointConstant;
import com.dmap.analysispoint.po.BurialPoint;
import com.dmap.analysispoint.service.AddBurialPointInfoService;
import com.dmap.analysispoint.utils.VersionUtils;
import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* @author:zhouhao
 * \* Date:23/05/2018
 * \* Description:
 * \*
 */
@Api(value = "插入埋点信息", description = "api/插入埋点信息")
@Controller
@RequestMapping(value = "/api/dmap-analysis-point-server/v2/")

public class BurialPointController {

    @Resource
    private AddBurialPointInfoService addBurialPointInfoService;

    @ResponseBody
    @ApiOperation(value = "插入基础的埋点信息",
            notes = "1.传入所要查询的参数，其中用户ID、手机唯一标识、设备唯一标识、公司编号、渠道、项目编号、设备固件编号、设备固件版本号、app版本号、设备型号必传\n" +
                    "2.返回插入的记录条数")
    @RequestMapping(value = "insertBaseBurialPointInfo", method = RequestMethod.POST)
    public BaseVo insertBaseBurialPointInfo(@RequestBody List<BurialPointBo> param) {
        BaseVo baseVo = new BaseVo();
        if (CollectionUtils.isEmpty(param)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
        }
        List<BurialPoint> burialPointList = new ArrayList<>();
        for (BurialPointBo burialPointBo : param) {
            BurialPoint burialPoint = new BurialPoint();
            if (burialPointBo.getUserId() < 0) {
                continue;
            } else {
                if (burialPointBo.getUserTime() == null || burialPointBo.getUserTime() > 1000000000) {
                    burialPointBo.setUserTime(0);
                }
                if (burialPointBo.getBoundTime() == null || burialPointBo.getBoundTime() > 1000000000) {
                    burialPointBo.setBoundTime(0);
                }
                burialPoint.setProjectId(burialPointBo.getProjectId());
                burialPoint.setPlaceId(burialPointBo.getPlaceId());
                burialPoint.setAppVersion(burialPointBo.getAppVersion());
                burialPoint.setFirmwareId(burialPointBo.getFirmwareId());
                if (StringUtils.isNotBlank(burialPointBo.getFirmwareVersion())) {
                    burialPoint.setFirmwareVersion(VersionUtils.setFirmwareVersion(burialPointBo.getFirmwareVersion()));
                }else {
                    burialPoint.setFirmwareVersion(burialPointBo.getFirmwareVersion());
                }
                burialPoint.setEquipmentType(burialPointBo.getEquipmentType());
                burialPoint.setImei(burialPointBo.getImei());
                burialPoint.setBoundTime(burialPointBo.getBoundTime());
                if (!StringUtils.isBlank(burialPointBo.getLatitude())) {
                    burialPoint.setLatitude(Double.valueOf(burialPointBo.getLatitude()));
                }else {
                    burialPoint.setLatitude(0.0);
                }
                burialPoint.setLoginTime(burialPointBo.getLoginTime());
                if (!StringUtils.isBlank(burialPointBo.getLongitude())) {
                    burialPoint.setLongitude(Double.valueOf(burialPointBo.getLongitude()));
                }else {
                    burialPoint.setLongitude(0.0);
                }
                burialPoint.setPhoneLanguage(burialPointBo.getPhoneLanguage());
                burialPoint.setPhoneModel(burialPointBo.getPhoneModel());
                burialPoint.setPhoneType(burialPointBo.getPhoneType());
                burialPoint.setPhoneVersion(burialPointBo.getPhoneVersion());
                burialPoint.setSn(burialPointBo.getSn());
                if (burialPointBo.getStartUpNum() > 99) {
                    burialPoint.setStartUpNum(1);
                } else {
                    burialPoint.setStartUpNum(burialPointBo.getStartUpNum());
                }
                burialPoint.setUserId(burialPointBo.getUserId());
                if (burialPointBo.getUserTime() > 99999) {
                    burialPoint.setStartUpNum(1000);
                } else {
                    burialPoint.setUserTime(burialPointBo.getUserTime());
                }
                burialPoint.setUserType(burialPointBo.getUserType());
            }
            burialPointList.add(burialPoint);
        }

        int success = addBurialPointInfoService.insertBaseBurialPointInfo(burialPointList);
        if (success == 0) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        baseVo.setData(success);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "插入基础的埋点信息(测试用)",
            notes = "1.参数start为数据开始的第一个数，day为距离当天的时间，如0则为当天时间此时数据，1为昨天此时数据，2为前天此时数据\n" +
                    "2.返回插入记录条数")
    @RequestMapping(value = "insert/bech", method = RequestMethod.GET)
    public BaseVo insertBaseBurialPointInfoBetch(@RequestParam(value = "start") Integer start,
                                                 @RequestParam(value = "day") Integer day) {
        BaseVo baseVo = new BaseVo();

        List<BurialPoint> burialPointList = new ArrayList<>();
        int size = start + 500;
        for (int i = start; i < size; i++) {
            BurialPoint param = new BurialPoint();
            param.setUserId(Long.valueOf(i));
            param.setImei(String.valueOf(i));
            param.setSn(String.valueOf(i));
            param.setPlaceId(1004);
            param.setProjectId("AP008");
            param.setFirmwareId("a123");
            param.setFirmwareVersion("key1.5");
            param.setAppVersion("ipwerful1.3");
            param.setPhoneType(String.valueOf(i));
            param.setPhoneModel(String.valueOf(i));
            param.setPhoneVersion("ios13.11.7");
            param.setEquipmentType(0);
            param.setPhoneLanguage(String.valueOf(i));
            param.setStartUpNum(Math.toIntExact(i));
            param.setUserTime(10);
            param.setBoundTime(10);
            param.setLongitude(Double.valueOf(10));
            param.setLatitude(Double.valueOf(10));
            param.setLoginTime(System.currentTimeMillis() / 1000 - BurialPointConstant.ONE_DAY_TIME_MILLISECONDS * day);
            param.setUserType(0);
            burialPointList.add(param);

        }
        for (int i = start; i < size; i++) {
            long n = i + 500;
            BurialPoint param = new BurialPoint();
            param.setUserId(n);
            param.setImei(String.valueOf(n));
            param.setSn(String.valueOf(n));
            param.setPlaceId(1004);
            param.setProjectId("AP008");
            param.setFirmwareId("a123");
            param.setFirmwareVersion("key1.6");
            param.setAppVersion("ipwerful1.4");
            param.setPhoneType(String.valueOf(n));
            param.setPhoneModel(String.valueOf(n));
            param.setPhoneVersion("android13.11.7");
            param.setEquipmentType(0);
            param.setPhoneLanguage(String.valueOf(n));
            param.setStartUpNum(Math.toIntExact(n));
            param.setUserTime(10);
            param.setBoundTime(10);
            param.setLongitude(Double.valueOf(10));
            param.setLatitude(Double.valueOf(10));
            param.setLoginTime(System.currentTimeMillis() / 1000 - BurialPointConstant.ONE_DAY_TIME_MILLISECONDS * day);
            param.setUserType(0);
            burialPointList.add(param);
        }
        int success = addBurialPointInfoService.insertBaseBurialPointInfo(burialPointList);
        baseVo.setData(success);
        return baseVo;
    }

}
